基本类型
类型转换
字符串处理
来源:https://segmentfault.com/a/1190000019439223
空
排序
空值判断
字符串
|
|
"" 一般是判断一个string字符串是否为空 // 例子: var test string if t == "" { fmt.Println("string test is empty !") } |
nil
|
|
// nil 一般是判断结构体的指针是否为空 type test struct { Name string Password string } func main() { var data *test if data == nil { fmt.Println("struct data is empty !") } } |
len(t) 一般用于求数组、切片的长度的时候
|
|
func main() { var test []string if len(test) == 0 { fmt.Println("[]string test is empty !") } } |
类型转换
(1)int转string
|
1
2
|
s := strconv.Itoa(i)
等价于s := strconv.FormatInt(int64(i), 10)
|
(2)int64转string
|
1
2
|
i := int64(123)
s := strconv.FormatInt(i, 10)
|
第二个参数为基数,可选2~36
注:对于无符号整形,可以使用FormatUint(i uint64, base int)
(3)string转int
|
1
|
i, err := strconv.Atoi(s)
|
(4)string转int64
|
1
|
i, err := strconv.ParseInt(s, 10, 64)
|
第二个参数为基数(2~36),第三个参数位大小表示期望转换的结果类型,其值可以为0, 8, 16, 32和64,分别对应 int, int8, int16, int32和int64
(5)float相关
float转string:
|
1
2
|
v := 3.1415926535
s1 := strconv.FormatFloat(v, 'E', -1, 32)
|
「三年博客,如果觉得我的文章对您有用,请帮助本站成长」
共有 0 - go基本操作